﻿@model IEnumerable<Superstore.Store.Models.ShoppingCartGoodsInfo>

<section id="shopping" class="Shopping">
    <section class="ShopPart1">
        <div class="ShoppingTxt fll">
            <div class="ShoppingCar">
                <img src="/images/ShoppingCar.png">
                @*<span>0</span>*@
            </div>
            <div class="ShoppingMoney">
                <span id="ShoppingHelper">购物车是空的</span>
            </div>
        </div>
        <div class="ShoppingAll fll">
            <a>满￥<span class="FlagFallPrice">30.00</span>免配送费</a>
        </div>
    </section>

    <section class="ShopPart2 ShopPart">
        <div class="ShoppingTxt fll">
            <div class="ShoppingCar" onclick="showShoppingDetail()">
                <img src="/images/ShoppingCar.png">
                <span id="ShoppingCarTotalNum1">0</span>
            </div>
            <div class="ShoppingMoney">
                <span>共</span><a id="ShoppingCarTotalPrice1">￥26.00</a>
            </div>
        </div>
        <div class="ShoppingAll fll" onclick="prePay()">
            <a>
                <span>去结算</span>
                <span id="ShoppingCarDelivery1">差￥26.00免配送费</span>
            </a>
        </div>
    </section>

    <section class="ShopPart3 ShopPart">
        <section class="ShopCar">
            <div class="ShopCarTop">
                <div class="ShoppingCar">
                    <img src="/images/ShoppingCar.png">
                    <span id="ShoppingCarTotalNum2">0</span>
                </div>
                <a onclick="resetShopping()">清空购物车</a>
            </div>
            <div class="ShopCarCon">
                <table id="table_goodsList">
                    </table>
                </div>
            </section>
            <div style="overflow:hidden;">
                <div class="ShoppingTxt fll" onclick="hideShoppingCar()">
                    <div class="ShoppingMoney">
                        <span>共</span><a id ="ShoppingCarTotalPrice2">￥26.00</a>
                    </div>
                </div>
                <div class="ShoppingAll fll" onclick="prePay()">
                    <a>
                        <span>去结算</span>
                        <span id="ShoppingCarDelivery2">差￥26.00免配送费</span>
                    </a>
                </div>
            </div>
    </section>

</section>
<script>
    $(function () {
        adjustShoppingInfo();
        loadShoppingCarGoodsList();
    });
    function showShoppingCar() {
        $('.OrderGray').show();
        $('.Shopping').attr("style", "display:block;z-index:999;");
        $('body').css('overflow', 'hidden');
    }
    function hideShoppingCar() {
        $('.OrderGray').hide();
        adjustShoppingInfo();
        $('body').css('overflow', 'scroll');
    }
    function loadShoppingCarGoodsList() {
        $("table#table_goodsList tr").each(function () {
            $(this).remove();
        });
        var shoppingList = ShoppingMgr.getShoppingList();
        for (index in shoppingList) {
            if (shoppingList[index].goods_num > 0) {
                var tr = $(
                    '<tr id=\"tr_' + shoppingList[index].goods_info.goodsid + '\">' +
                    '<td style=\"width:50%;\">' + shoppingList[index].goods_info.name + '</td>' +
                    '<td style=\"width:20%;\">￥' + shoppingList[index].goods_info.price + '</td>' +
                    '<td style=\"width:30%;\"><div class=\"IndexGoodsNum\">' +
                    '<a class=\"dec_shoping_count_' + shoppingList[index].goods_info.goodsid + '\" onclick=\"adjustNum2(' + shoppingList[index].goods_info.goodsid + ',' + shoppingList[index].main_tab + ',' + shoppingList[index].sub_tab + ', -1)\"><img src=\"/images/Dec.png\"></a>' +
                '<span id=\"shoping_goodsid_' + shoppingList[index].goods_info.goodsid + '\">' + shoppingList[index].goods_num+'</span>' +
                '<a class=\"add_shoping_count_' + shoppingList[index].goods_info.goodsid + '\" onclick=\"adjustNum2(' + shoppingList[index].goods_info.goodsid + ',' + shoppingList[index].main_tab + ',' + shoppingList[index].sub_tab + ', 1)\"><img src=\"/images/Add.png\"></a>' +
                '</div></td></tr>');
                $("table#table_goodsList").append(tr);
            }
        }
    }

    function showShopPart1() {
        $('.ShopPart1').show();
        var flagFallPrice = TerminalMgr.getFlagFallPrice();
        $('#FlagFallPrice').text(flagFallPrice);
    }

    function hideShopPart1() {
        $('.ShopPart1').hide();
    }

    function showShopPart2() {
        var totalNum = ShoppingMgr.getTotalNum();
        var totalPrice = ShoppingMgr.getTotalPrice();
        //console.log(" totalPrice:" + totalPrice + " totalNum:" + totalNum);
        $('.ShopPart2').show();
        $('#ShoppingCarTotalPrice1').html("￥" + totalPrice.toFixed(1));
        $('#ShoppingCarTotalNum1').text(totalNum);

        showDeliveryFee(totalPrice, "ShoppingCarDelivery1");
    }

    function hideShopPart2() {
        $('.ShopPart2').hide();
    }

    function showShopPart3() {
        var totalNum = ShoppingMgr.getTotalNum();
        var totalPrice = ShoppingMgr.getTotalPrice();
        $('.ShopPart3').show();
        $('#ShoppingCarTotalPrice2').html("￥" + totalPrice.toFixed(1));
        $('#ShoppingCarTotalNum2').text(totalNum);

        loadShoppingCarGoodsList();

        showDeliveryFee(totalPrice, "ShoppingCarDelivery2");
    }

    function hideShopPart3() {
        $('.ShopPart3').hide();
    }
    //显示运费金额
    function showDeliveryFee(totalPrice, showID) {
        var flagFallPrice = TerminalMgr.getFlagFallPrice();
        var deliveryHelper;
        if (totalPrice < flagFallPrice) {
            deliveryHelper = "差￥";
            deliveryHelper += (flagFallPrice - totalPrice).toFixed(1);
            deliveryHelper += "免配送费";
            $('#' + showID).text(deliveryHelper);
        } else {
            deliveryHelper = "免配送费";
            $('#' + showID).text(deliveryHelper);
        }
    }

    function adjustShoppingInfo() {
        $('.OrderGray').hide();
        var totalNum = ShoppingMgr.getTotalNum();
        //console.log(" kk+totalNum:" + totalNum);
        if (totalNum == 0) {
            showShopPart1();
            hideShopPart2();
        } else {
            hideShopPart1();
            showShopPart2();
        }
        hideShopPart3();
    }

    function prePay() {
        window.location.href = '@Url.Content("~/")' + 'Buy/Index';
    }

    function resetShopping() {
        ShoppingMgr.resetShoppingMgr();
        hideShopPart2();
        hideShopPart3();
        showShopPart1();

        $('.ShopCarCon table').empty();
        $('.ShoppingCar span').text(ShoppingMgr.getTotalNum());
        $('.IndexPart .IndexGoodsNum').each(function (index) {
            $('.IndexPart .IndexGoodsNum span').text('0');
            $('.IndexPart .IndexGoodsNum span').hide();
            $('.IndexPart .IndexGoodsNum a').eq(2 * index).hide();
        });
        
        hideShoppingCar();
    }

    function showShoppingDetail() {
        hideShopPart1();
        hideShopPart2();
        showShopPart3();
        showShoppingCar();
    }
    var StatusType;
    function adjustShopListShow(goodsid, goodsNum) {
        if (goodsNum > 0) {
            $(".dec_shoping_count_" + goodsid).show();
            $("#shoping_goodsid_" + goodsid).show();
            $("#shoping_goodsid_" + goodsid).text(goodsNum);
        } else {
            $("#tr_" + goodsid).parent().remove();
            var totalNum = ShoppingMgr.getTotalNum();
            //console.log("adjust-totalNum:" + totalNum);
            if (totalNum == 0) {
                showShopPart1();
                hideShopPart2();
                hideShopPart3();
                hideShoppingCar();
            }
            else {
                showShopPart3();
                hideShopPart1();
                hideShopPart2();
            }
        }
    }
    //函数名末尾的2是为了与商品展示页面中的函数名区分开来
    function adjustGoodsNumShow2(goodsid, goodsNum, shoppingList) {
        adjustGoodsListShow(goodsid, goodsNum);
        //adjustPromotionGoodsListShow(goodsid, goodsNum);
        adjustShopListShow(goodsid, goodsNum);
        var totalPrice = ShoppingMgr.getTotalPrice();
        $('#ShoppingCarTotalPrice1').html("￥" + totalPrice.toFixed(1));
        $('#ShoppingCarTotalPrice2').html("￥" + totalPrice.toFixed(1));
        $('.ShoppingCar span').text(ShoppingMgr.getTotalNum());

        showDeliveryFee(totalPrice, "ShoppingCarDelivery1");
        showDeliveryFee(totalPrice, "ShoppingCarDelivery2");
    }
    //函数名末尾的2是为了与商品展示页面中的函数名区分开来
    function adjustNum2(goodsId, mainTab, subTab, typeOp) {
        ShoppingMgr.adjustGoodsNum(goodsId, mainTab, subTab, typeOp, adjustGoodsNumShow2);
    }
</script>